package com.rongke.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.rongke.commons.JsonResp;
import com.rongke.mapper.DeliveryPlanMapper;
import com.rongke.model.DeliveryItem;
import com.rongke.model.DeliveryPlan;
import com.rongke.model.Message;
import com.rongke.service.DeliveryItemService;
import com.rongke.service.DeliveryPlanService;

import com.rongke.utils.StringUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.util.Date;
import java.util.List;

/**
 * @DeliveryPlanController
 * @配送方式Controller
 * @version : Ver 1.0
 */
@RestController
@RequestMapping(value="/api/deliveryPlan")
@Transactional
@CrossOrigin
public class DeliveryPlanController {
    private Logger log = Logger.getLogger(this.getClass());
    @Autowired
    private DeliveryPlanService deliveryPlanService;
    @Autowired
    private DeliveryItemService deliveryItemService;

    /**
     * @添加配送方式
     * @param deliveryPlan
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/add", method = RequestMethod.POST)
    public JsonResp addDeliveryPlan(@RequestBody DeliveryPlan deliveryPlan){
        log.debug("添加配送方式");

        List<DeliveryItem> items=deliveryPlan.getDeliveryItems();
        //新增
        if(null==deliveryPlan.getId()){
            EntityWrapper<DeliveryPlan> ew=new EntityWrapper<>();
            ew.setSqlSelect("max(num)");
            Integer num= (Integer) deliveryPlanService.selectObj(ew);
            if(null==num){
                num=0;
            }else num+=1;
            deliveryPlan.setNum(num);
            deliveryPlan.setGmtDatetime(new Date());
            deliveryPlanService.insert(deliveryPlan);
            items.forEach(e->{
                e.setNum(deliveryPlan.getNum());
            });
            deliveryItemService.insertBatch(items);
        }else{
            //修改
            deliveryPlanService.updateById(deliveryPlan);
            deliveryItemService.updateBatchById(items);
        }

        return JsonResp.ok(deliveryPlan);
    }

    /**
     * @修改配送方式
     * @param deliveryPlan
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/update", method = RequestMethod.POST)
    public JsonResp updateDeliveryPlan(@RequestBody DeliveryPlan deliveryPlan){
        log.debug("修改配送方式");
        deliveryPlanService.updateById(deliveryPlan);
        return JsonResp.ok(deliveryPlan);
    }
    /**
     * @根据id查找配送方式
     * @param id
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/selectOne", method = RequestMethod.GET)
    public JsonResp selectDeliveryPlan(Long id){
        log.debug("查找配送方式");
        DeliveryPlan deliveryPlan = deliveryPlanService.selectById(id);
        EntityWrapper<DeliveryItem> ewi=new EntityWrapper<>();
        ewi.eq("num",deliveryPlan.getNum());
        List<DeliveryItem> items=deliveryItemService.selectList(ewi);
        deliveryPlan.setDeliveryItems(items);
        return JsonResp.ok(deliveryPlan);
    }

    /**
     * @根据条件查找配送方式
     * @param deliveryPlan
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/selectByCondition", method = RequestMethod.POST)
    public JsonResp selectDeliveryPlanByCondition(@RequestBody DeliveryPlan deliveryPlan){
        log.debug("根据条件查找配送方式");
        if(deliveryPlan==null)
            return JsonResp.toFail("参数不能为空");
        List<DeliveryPlan> deliveryPlanList=deliveryPlanService.selectByCondition(deliveryPlan);
        return JsonResp.ok(deliveryPlanList);
    }

    /**
     * 后台获取邮费方案列表
     * @param page
     * @return
     */
    @RequestMapping(value = "/fetchDeliPlansAdmin",method = RequestMethod.POST)
    public JsonResp fetchDeliPlansAdmin(@RequestBody Page<DeliveryPlan> page){
        log.debug("后台获取邮费方案列表");
        String search=page.getCondition().get("searchText").toString();
        EntityWrapper<DeliveryPlan> ew=new EntityWrapper<>();
        if(StringUtil.isNotEmpty(search)){
            ew.like("title",search);
        }
        page.setCondition(null);
        ew.orderBy("gmt_datetime",false);
        page=deliveryPlanService.selectPage(page,ew);
        page.getRecords().forEach(e->{
            EntityWrapper<DeliveryItem> ewi=new EntityWrapper<>();
            ewi.eq("num",e.getNum());
            List<DeliveryItem> items=deliveryItemService.selectList(ewi);
            e.setDeliveryItems(items);
        });
        return JsonResp.ok(page);
    }

    /**
     * 商品上传时可选邮费方案
     * @return
     */
    @RequestMapping(value="/getGoodsAddChosenPlans", method = RequestMethod.GET)
    public JsonResp getGoodsAddChosenPlans(){
        log.debug("商品上传时可选邮费方案");
        EntityWrapper<DeliveryPlan> ew=new EntityWrapper<>();
        List<DeliveryPlan> items=deliveryPlanService.selectList(ew);
       return JsonResp.ok(items);
    }

}
